import Vue from 'vue';

const WebSocketPlugin = {
    install(Vue, options) {
        const ws = new WebSocket('ws://47.92.76.83:8506/infra/ws?token=1b15afaf49ed4a3496f1d5af02ea9b9b');

        Vue.prototype.$websocket = {
            send(message) {
                if (ws.readyState === WebSocket.OPEN) {
                    ws.send(message);
                }
            },
            onMessage(callback) {
                ws.onmessage = callback;
            },
            onOpen(callback) {
                ws.onopen = callback;
            },
            onClose(callback) {
                ws.onclose = callback;
            },
            onError(callback) {
                ws.onerror = callback;
            }
        };

        Vue.mixin({
            created() {
                if (this.$options.websocket) {
                    this.$websocket.onMessage = this.$options.websocket.onMessage;
                    this.$websocket.onOpen = this.$options.websocket.onOpen;
                    this.$websocket.onClose = this.$options.websocket.onClose;
                    this.$websocket.onError = this.$options.websocket.onError;
                }
            },
            beforeDestroy() {
                if (this.$options.websocket) {
                    this.$websocket.onMessage = null;
                    this.$websocket.onOpen = null;
                    this.$websocket.onClose = null;
                    this.$websocket.onError = null;
                }
            }
        });
    }
};

export default WebSocketPlugin;
